# !/usr/bin/env python3
from flask import Flask
from login import login
from flask import request
from config import app,db
from wechat_user.models import USER
from wechat_user.WXBizDataCrypt import WXBizDataCrypt
from wechat_user.main import user
import logging


@app.route('/hello' , methods = ['POST', 'GET'])
def hello():
    form = request.form if request.method == 'POST' else request.args
    return dict(data=form.get('d'),message='ok')

@app.route('/new/<do>/<sno>/<spw>')
def get(do, sno, spw):
    sno = 16008062
    spw = '971016w'
    info = login(sno, spw, do)
    return info

# @app.route('/user/save')
# def save_user():
#     form = request.form if request.method == 'POST' else request.args
#     openid = form.get('openid','')
#     sno = form.get('yktid',0)
#     spw = form.get('passwd','')
#     do = form.get('do', '')
#     year = form.get('year', '')
#     term = form.get('term', '')
#     res = user.bind(openid=openid,sno=sno,spw=spw,do=do,year=year,term=term)
#     return res

# @app.route('/user/get_info')
# def get_user_info():
#     form = request.form if request.method == 'POST' else request.args
#     code = form.get('code')
#     key = form.get('key')
#     iv = form.get('iv')
#     logging.error(iv)
#     return user.get_user_info(code=code,key=key,iv=iv)

@app.route('/user/save_sno')
def save_sno():
    form = request.form if request.method == 'POST' else request.args
    openid = form.get('openid','')
    sno = form.get('yktid',0)
    spw = form.get('passwd','')
    do = form.get('do','')
    year = form.get('year','')
    term = form.get('term','')
    user = USER.query.filter(USER.openid==openid).first()
    user.sno = sno
    user.spw = spw
    db.session.commit()
    return dict(message='ok',status=200)

@app.route('/get/mark')
def get_mark():
    form = request.form if request.method == 'POST' else request.args
    openid = form.get('openid', '')
    res = user.mark(openid=openid)
    return dict(data=res,message='ok',status=200)
